package com.zecan.blog.config;

import org.elasticsearch.client.RestHighLevelClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.elasticsearch.client.ClientConfiguration;
import org.springframework.data.elasticsearch.client.RestClients;
import org.springframework.data.elasticsearch.config.AbstractElasticsearchConfiguration;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;

/**
 * @ClassName ElasticConfg
 * @Description
 * @date 2022/11/18 23:28
 * @Version 1.0
 */
@Configuration
public class ElasticConfig extends AbstractElasticsearchConfiguration {

    @Override
    @Bean
    public RestHighLevelClient elasticsearchClient() {
        final ClientConfiguration clientConfiguration = ClientConfiguration.builder()

                .connectedTo("139.224.69.204:9200")
                //.connectedTo("localhost:9200")
                .build();
        return RestClients.create(clientConfiguration).rest();
    }

    // 注入ElasticsearchRestTemplate 需要 RestHighLevelClient也一起注入
    @Bean
    public ElasticsearchRestTemplate elasticsearchRestTemplate() {

        return new ElasticsearchRestTemplate(elasticsearchClient());
    }
}
